<?php

    $dir = pathinfo($_SERVER['SCRIPT_FILENAME']); 

    if (ini_set("include_path",ini_get("include_path").((strtoupper(PHP_OS) == "LINUX") ? ":" : ";").$dir['dirname']) === false) {
            echo "ERROR: Definicion en el include_path";
            exit;
    }

    include('../adodb/adodb.inc.php');
    include_once('../adodb/adodb-pager.inc.php');
    include_once('../conf/dbconn_param.php');
    include_once('FetchUtil.php');

    //Busca en una matriz y devuelve una submatriz
    function search($array, $key, $value)
    {
        $results = array();

        if (is_array($array))
        {
            if (isset($array[$key]) && $array[$key] == $value)
                $results[] = $array;

            foreach ($array as $subarray)
                $results = array_merge($results, search($subarray, $key, $value));
        }

        return $results;
    }    
    
    /*
       $arr = array(0 => array(id=>1,name=>"cat 1"),
       1 => array(id=>2,name=>"cat 2"),
       2 => array(id=>3,name=>"cat 1"));

       print_r(search($arr, 'name', 'cat 1'));

     */
    
    
    
    $db = ADONewConnection($dbdriver);
    $db->Connect($server, $user, $password, $database);
    $db->SetFetchMode(ADODB_FETCH_ASSOC);

    $dsn = "$dbdriver://$user:$password@$server/$database?clientflags=65536"; 
    $db_sp = new FetchUtil($dsn);

    $productoId = $_REQUEST['productoId'];
    $bodegaId   = $_REQUEST['bodegaId'];
    $tallosBunche = $_REQUEST['tallosBunche'];
    $fechaCorte = $_REQUEST['fechaCorte'];
    
    
    
    $db->debug = false;
	
    $activo = 1;
    
    /*Longitudes*/
    $sql1 = "SELECT longitud_id, largo FROM longitud ORDER BY largo DESC";

    $rs1 = $db->Execute($sql1);
    
    $datos1 = $rs1->GetRows();

    /*llamada al store procedure*/
    $rows_sp = $db_sp->fetchAll("CALL GetDisponibilidad 
            ($productoId,$bodegaId,$tallosBunche,'$fechaCorte')");
    
    $i = 0;
    $j = 0;
    //Actualizamos la disponibilidad por variedad 
    foreach ($datos2 as $key => $row) {
        
        $result = search($datos3, 'variedad_id', $row['variedad_id']);
        if ( count($result) > 0)
        {
            $datos2[$j]['dispo'] = $row['dispo'] - $result[0]['reserva'];
        }
        $j++;
    }  
    
    
    echo "<table id=\"tblDisp\" border=\"0\">";
    echo "<thead>";
    echo "<th >ID</th>";
    echo "<th >VARIEDAD</th>";
    echo "<th >DISP</th>";
    
    foreach($datos1 as $data_) {
        echo "<th >".$data_['largo']."D</th>";
        //echo "<th >".$data_['largo']."R</th>";
    }
    echo "<thead>";
    echo "<tbody>";
    
    $x = 1;
    foreach($rows_sp as $rdata_) {
            
        if($x == 1){
            echo "<tr id=\"".$rdata_['variedad_id']."\">";
            echo "<td>".$rdata_['variedad_id']."</td>";
            echo "<td>".$rdata_['nombre']."</td>";
            //Restar dispo y reservado por variedad 
            echo "<td>".$rdata_['total']."</td>";
            
            echo "<td title=\"".$rdata_['dispo']."\" class=\"editable\" >0</th>";
            //echo "<td>0</th>";

        }
        else{
            echo "<td title=\"".$rdata_['dispo']."\" class=\"editable\" >0</th>";
            //echo "<td>0</th>";
        }

        if ($x == count($datos1) and $x > 1 ){
            $x = 1;
            echo "</tr>";
        }else
        {
            $x++;
        }
    }
    
    echo "</tbody>";    
    echo "</table>";

    
   $db->Close();
    // $db_sp->Close();
?>	